using UnityEngine;
using UnityEngine.AI;
using UnityEngine.Networking;

[RequireComponent(typeof(CharacterSystem))]
[RequireComponent(typeof(NavMeshAgent))]
public class AICharacterControllerNav : NetworkBehaviour
{
	[Header("Properties")]
	public string[] TargetTag = new string[1]
	{
		"Player"
	};

	public float AIUpdateRate = 8f;

	public GameObject ObjectTarget;

	[HideInInspector]
	public Vector3 PositionTarget;

	[HideInInspector]
	public CharacterSystem character;

	[HideInInspector]
	public float DistanceAttack = 2f;

	[Header("Attacking")]
	public float DistanceMoveTo = 20f;

	public float TurnSpeed = 10f;

	public bool BrutalMode;

	public bool RushMode;

	public float PatrolRange = 10f;

	[HideInInspector]
	public Vector3 positionTemp;

	[HideInInspector]
	public int aiTime;

	[HideInInspector]
	public int aiState;

	private float attackTemp;

	public float AttackDelay = 0.5f;

	[Header("Sound")]
	public float IdleSoundDelay = 10f;

	private float soundTime;

	private float soundTimeDuration;

	private AIManager AImange;

	private NavMeshAgent navAgent;

	private Vector3 targetDirectiom;

	private float timeTmp;

	private void Start()
	{
		character = base.gameObject.GetComponent<CharacterSystem>();
		navAgent = base.gameObject.GetComponent<NavMeshAgent>();
		navAgent.avoidancePriority = Random.Range(0, 100);
		positionTemp = base.transform.position;
		aiState = 0;
		attackTemp = Time.time;
		soundTime = Time.time;
		soundTimeDuration = Random.Range(0f, IdleSoundDelay);
		character.NetworkNetID = -1;
	}

	public void Attack(Vector3 targetDirectiom)
	{
		if (Time.time > attackTemp + AttackDelay)
		{
			character.DoDamage();
			character.AttackAnimation();
			attackTemp = Time.time;
		}
	}

	public void AIDoAttack()
	{
		if (Time.time > attackTemp + AttackDelay)
		{
			character.DoDamage();
			character.AttackAnimation();
			attackTemp = Time.time;
		}
	}

	private void Update()
	{
		if (character == null)
		{
			return;
		}
		if (Time.time > soundTime + soundTimeDuration)
		{
			character.PlayIdleSound();
			soundTimeDuration = Random.Range(0f, IdleSoundDelay);
			soundTime = Time.time;
		}
		if (!base.isServer || !character.IsAlive)
		{
			return;
		}
		float num = 1f / Time.deltaTime;
		float num2 = num / AIUpdateRate * Time.deltaTime;
		if (Time.time > timeTmp + num2)
		{
			timeTmp = Time.time;
			character.isServerControl = true;
			DistanceAttack = character.PrimaryWeaponDistance;
			float num3 = Vector3.Distance(PositionTarget, base.gameObject.transform.position);
			targetDirectiom = PositionTarget - base.transform.position;
			Quaternion b = base.transform.rotation;
			float t = TurnSpeed * Time.time;
			if (targetDirectiom != Vector3.zero)
			{
				b = Quaternion.LookRotation(targetDirectiom);
				b.x = 0f;
				b.z = 0f;
				base.transform.rotation = Quaternion.Lerp(base.transform.rotation, b, t);
			}
			if (ObjectTarget != null)
			{
				DamageManager component = ObjectTarget.GetComponent<DamageManager>();
				PositionTarget = ObjectTarget.transform.position;
				if (aiTime <= 0)
				{
					aiState = Random.Range(0, 4);
					aiTime = Random.Range(10, 100);
				}
				else
				{
					aiTime--;
				}
				if (num3 <= DistanceAttack)
				{
					if (aiState == 0 || BrutalMode)
					{
						Attack(targetDirectiom);
					}
				}
				else if (num3 <= DistanceMoveTo)
				{
					base.transform.rotation = Quaternion.Lerp(base.transform.rotation, b, t);
				}
				else
				{
					ObjectTarget = null;
					if (aiState == 0)
					{
						aiState = 1;
						aiTime = Random.Range(10, 500);
						PositionTarget = positionTemp + new Vector3(Random.Range(0f - PatrolRange, PatrolRange), 0f, Random.Range(0f - PatrolRange, PatrolRange));
					}
				}
				if ((bool)component && !component.IsAlive)
				{
					ObjectTarget = null;
				}
			}
			else
			{
				float num4 = float.MaxValue;
				for (int i = 0; i < TargetTag.Length; i++)
				{
					TargetCollector targetCollector = UnitZ.aiManager.FindTargetTag(TargetTag[i]);
					if (targetCollector == null)
					{
						continue;
					}
					GameObject[] targets = targetCollector.Targets;
					if (targets == null || targets.Length <= 0)
					{
						continue;
					}
					for (int j = 0; j < targets.Length; j++)
					{
						DamageManager component2 = targets[j].GetComponent<DamageManager>();
						if (component2 != null && component2.IsAlive)
						{
							float num5 = Vector3.Distance(targets[j].gameObject.transform.position, base.gameObject.transform.position);
							if (num5 <= num4 && (num5 <= DistanceMoveTo || num5 <= DistanceAttack || RushMode) && ObjectTarget != targets[j].gameObject)
							{
								num4 = num5;
								ObjectTarget = targets[j].gameObject;
							}
						}
					}
				}
				if (aiState == 0)
				{
					aiState = 1;
					aiTime = Random.Range(10, 200);
					PositionTarget = positionTemp + new Vector3(Random.Range(0f - PatrolRange, PatrolRange), 0f, Random.Range(0f - PatrolRange, PatrolRange));
				}
				if (aiTime <= 0)
				{
					aiState = Random.Range(0, 4);
					aiTime = Random.Range(10, 200);
				}
				else
				{
					aiTime--;
				}
			}
		}
		if ((bool)navAgent)
		{
			navAgent.SetDestination(PositionTarget);
			navAgent.speed = character.GetCurrentMoveSpeed();
		}
	}

	private void UNetVersion()
	{
	}

	public override bool OnSerialize(NetworkWriter writer, bool forceAll)
	{
		bool result = default(bool);
		return result;
	}

	public override void OnDeserialize(NetworkReader reader, bool initialState)
	{
	}
}
